package com.zhang.order.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhang.order.dto.OrderItemSearchDTO;
import com.zhang.order.entity.OrderItem;
import com.zhang.order.mapper.OrderItemMapper;
import com.zhang.order.service.IOrderItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单项信息 服务实现类
 * </p>
 *
 * @author 创世神
 * @since 2023-12-05
 */
@Service
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements IOrderItemService {

    @Override
    public List<OrderItem> getListByOrderId(Long orderId) {
        LambdaQueryWrapper<OrderItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrderItem::getOrderId, orderId);
        return list(wrapper);
    }

    @Override
    public List<Long> getSkuIdListByOrderId(Long orderId) {
        List<OrderItem> listByOrderId = getListByOrderId(orderId);
        return listByOrderId.stream().map(OrderItem::getSkuId).collect(Collectors.toList());
    }

    @Override
    public IPage<OrderItem> getBusinessOrderItems(OrderItemSearchDTO dto) {
        Page<OrderItem> page = new Page<>(dto.getPageNum(), dto.getPageSize());
//        如果是超级管理员，则就查询全部
        if (!StpUtil.hasRole("admin")) {
            System.out.println("测试中");
            dto.setBusinessId(StpUtil.getLoginIdAsString());
        }
        else {
            dto.setBusinessId(null);
        }
//        否则查询与自己相关的订单项
        return baseMapper.getBusinessOrderItems(dto,page);
    }
}
